00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _vector_h_
00021 #define _vector_h_
00022
00023 #include <boost/scoped_ptr.hpp>
00024 #include <gridpack/parallel/distributed.hpp>
00025 #include <gridpack/utilities/uncopyable.hpp>
00026 #include <gridpack/math/vector_implementation.hpp>
00027 #include <gridpack/utilities/exception.hpp>
00028
00029 namespace gridpack {
00030 namespace math {
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 template <typename T, typename I = int>
00059 class VectorT
00060 : public parallel::WrappedDistributed,
00061 private utility::Uncopyable,
00062 public BaseVectorInterface<T, I>
00063 {
00064 protected:
00065
00066
00067 typedef class VectorImplementation<T, I> ImplType;
00068
00069 public:
00070
00071 typedef typename BaseVectorInterface<T, I>::IdxType IdxType;
00072 typedef typename BaseVectorInterface<T, I>::TheType TheType;
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 VectorT(const parallel::Communicator& comm, const int& local_length);
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 explicit VectorT(ImplType *vimpl)
00101 : parallel::WrappedDistributed(vimpl), utility::Uncopyable(),
00102 p_vector_impl(vimpl)
00103 { }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 ~VectorT(void)
00115 { }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 VectorT *clone(void) const
00128 {
00129 ImplType *pimpl_clone = p_vector_impl->clone();
00130 VectorT *result = new VectorT(pimpl_clone);
00131 return result;
00132 }
00133
00134
00135 VectorT *localClone(void) const
00136 {
00137 IdxType n(this->size());
00138 parallel::Communicator self(this->communicator().self());
00139 std::vector< TheType > x(n);
00140 this->getAllElements(&x[0]);
00141 VectorT *result(new VectorT(self, n));
00142 IdxType lo, hi;
00143 result->localIndexRange(lo, hi);
00144 result->setElementRange(lo, hi, &x[0]);
00145 result->ready();
00146 return result;
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 void add(const VectorT& x, const TheType& scale = 1.0);
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 void add(const TheType& x);
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 void equate(const VectorT& x);
00183
00184
00185 void elementMultiply(const VectorT& x);
00186
00187
00188 void elementDivide(const VectorT& x);
00189
00190 protected:
00191
00192
00193 boost::scoped_ptr<ImplType> p_vector_impl;
00194
00195
00196 IdxType p_size(void) const
00197 { return p_vector_impl->size(); }
00198
00199
00200 IdxType p_localSize(void) const
00201 { return p_vector_impl->localSize(); }
00202
00203
00204 void p_localIndexRange(IdxType& lo, IdxType& hi) const
00205 { p_vector_impl->localIndexRange(lo, hi); }
00206
00207
00208 void p_setElement(const IdxType& i, const TheType& x)
00209 { p_vector_impl->setElement(i, x); }
00210
00211
00212 void p_setElements(const IdxType& n, const IdxType *i, const TheType *x)
00213 { p_vector_impl->setElements(n, i, x); }
00214
00215
00216 void p_setElementRange(const IdxType& lo, const IdxType& hi, TheType *x)
00217 { p_vector_impl->setElementRange(lo, hi, x); }
00218
00219
00220 void p_addElement(const IdxType& i, const TheType& x)
00221 { p_vector_impl->addElement(i, x); }
00222
00223
00224 void p_addElements(const IdxType& n, const IdxType *i, const TheType *x)
00225 { p_vector_impl->addElements(n, i, x); }
00226
00227
00228 void p_addElementRange(const IdxType& lo, const IdxType& hi, TheType *x)
00229 { p_vector_impl->addElementRange(lo, hi, x); }
00230
00231
00232 void p_getElement(const IdxType& i, TheType& x) const
00233 { p_vector_impl->getElement(i, x); }
00234
00235
00236 void p_getElements(const IdxType& n, const IdxType *i, TheType *x) const
00237 { p_vector_impl->getElements(n, i, x); }
00238
00239
00240 void p_getElementRange(const IdxType& lo, const IdxType& hi, TheType *x) const
00241 { p_vector_impl->getElementRange(lo, hi, x); }
00242
00243
00244 void p_getAllElements(TheType *x) const
00245 { p_vector_impl->getAllElements(x); }
00246
00247
00248 void p_zero(void)
00249 { p_vector_impl->zero(); }
00250
00251
00252 void p_fill(const TheType& v)
00253 { p_vector_impl->fill(v); }
00254
00255
00256 void p_scale(const TheType& x)
00257 { p_vector_impl->scale(x); }
00258
00259
00260 double p_norm1(void) const
00261 { return p_vector_impl->norm1(); }
00262
00263
00264 double p_norm2(void) const
00265 { return p_vector_impl->norm2(); }
00266
00267
00268 double p_normInfinity(void) const
00269 { return p_vector_impl->normInfinity(); }
00270
00271
00272 void p_abs(void)
00273 { p_vector_impl->abs(); }
00274
00275
00276 void p_real(void)
00277 { p_vector_impl->real(); }
00278
00279
00280 void p_imaginary(void)
00281 { p_vector_impl->imaginary(); }
00282
00283
00284 void p_conjugate(void)
00285 { p_vector_impl->conjugate(); }
00286
00287
00288 void p_exp(void)
00289 { p_vector_impl->exp(); }
00290
00291
00292 void p_reciprocal(void)
00293 { p_vector_impl->reciprocal(); }
00294
00295
00296 void p_ready(void)
00297 { p_vector_impl->ready(); }
00298
00299
00300 void p_print(const char* filename = NULL) const
00301 { p_vector_impl->print(filename); }
00302
00303
00304 void p_save(const char *filename) const
00305 { p_vector_impl->save(filename); }
00306
00307
00308 void p_loadBinary(const char *filename)
00309 { p_vector_impl->loadBinary(filename); }
00310
00311
00312 void p_saveBinary(const char *filename) const
00313 { p_vector_impl->saveBinary(filename); }
00314
00315
00316 void p_accept(ImplementationVisitor& visitor)
00317 {
00318 p_vector_impl->accept(visitor);
00319 }
00320
00321
00322 void p_accept(ConstImplementationVisitor& visitor) const
00323 {
00324 p_vector_impl->accept(visitor);
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339 void p_checkCompatible(const VectorT& x) const
00340 {
00341
00342
00343
00344
00345 if (this->size() != x.size()) {
00346 throw gridpack::Exception("incompatible: sizes do not match");
00347 }
00348 }
00349 };
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 template <typename T, typename I>
00372 VectorT<T, I> *add(const VectorT<T, I>& A, const VectorT<T, I>& B)
00373 {
00374 VectorT<T, I> *result(A.clone());
00375 result->add(B);
00376 return result;
00377 }
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 template <typename T, typename I>
00399 VectorT<T, I> *abs(const VectorT<T, I>& x)
00400 {
00401 VectorT<T, I> *result(x.clone());
00402 result->abs();
00403 return result;
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414 template <typename T, typename I>
00415 VectorT<T, I> *real(const VectorT<T, I>& x)
00416 {
00417 VectorT<T, I> *result(x.clone());
00418 result->real();
00419 return result;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 template <typename T, typename I>
00432 VectorT<T, I> *imaginary(const VectorT<T, I>& x)
00433 {
00434 VectorT<T, I> *result(x.clone());
00435 result->imaginary();
00436 return result;
00437 }
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 template <typename T, typename I>
00448 VectorT<T, I>* conjugate(const VectorT<T, I>& x)
00449 {
00450 VectorT<T, I> *result(x.clone());
00451 result->conjugate();
00452 return result;
00453 }
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 template <typename T, typename I>
00472 void add(const VectorT<T, I>& A, const VectorT<T, I>& B, VectorT<T, I>& result)
00473 {
00474 result.equate(A);
00475 result.add(B);
00476 }
00477
00478
00479 typedef class VectorT<ComplexType> ComplexVector;
00480 typedef class VectorT<double> RealVector;
00481 typedef ComplexVector Vector;
00482
00483 }
00484 }
00485
00486 #endif